import Konva from "konva";
import { Draw } from "../../../Draw/Draw";
import { clearGuidLine } from "./group-guid-line";
import { IKonva } from "../../../Interface/IKonva";

/**
 * Konva Graph 基类拖拽结束事件
 * @param e
 * @param draw
 * @returns
 */
export function groupDragend(e: IKonva.Event, draw: Draw) {
  e.cancelBubble = true;

  const group = <Konva.Group>e.currentTarget;

  const layer = draw.getLayer();
  if (!layer) return;

  // 清空对齐辅助线
  clearGuidLine(draw);

  // 元素拖拽结束后，需要重新渲染以更新 layerImage，并进行图层缓存
  draw.render();

  // 清空 sx sy属性
  const selected = draw.getKonvaGraph().getSelected();
  selected.forEach((g) => g.setAttrs({ sx: null, sy: null }));

  // 广播
  draw.broadcast({
    type: `${group.id()}_groupDragEnd`,
    payload: {
      layerID: layer.id(),
      groupID: group.id(),
      x: group.x(),
      y: group.y(),
    },
  });
}
